package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.attachment.service.AttachmentInfoService;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.common.utils.CheckObjFields;
import com.css.fxfzypg.common.utils.UpdateUtil;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.entity.*;
import com.css.fxfzypg.modules.baseData.repository.SeismicSetoutRepository;
import com.css.fxfzypg.modules.baseData.service.SeismicSetoutService;
import com.css.fxfzypg.modules.baseData.queryParams.SeismicSetoutQueryParams;
import com.css.fxfzypg.modules.baseData.vo.ScSeismicSetoutVO;
import com.css.fxfzypg.modules.baseData.vo.ScSeismicSetoutVO1;
import com.css.fxfzypg.util.*;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class SeismicSetoutServiceImpl implements SeismicSetoutService {
    @Resource
    SeismicSetoutRepository seismicSetoutRepository;
    @Resource
    private SysAreaService sysAreaService;
    @Resource
    AttachmentInfoService attachmentInfoService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 修改SeismicSetout实体数据
     *
     * @param entity 实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse updateSeismicSetout(ScSeismicsetout entity) {
        //对数据校验
        StringBuilder message = getMessage1(entity);
        if (message.length() > 0) {
            return RestResponse.fail(String.valueOf(message));
        }
        ScSeismicsetout seismicSetoutById = seismicSetoutRepository.getSeismicSetoutById(entity.getScId());
        //把seismicSetoutById(通过Id查询)原实体，只赋值到entity目标实体的空值，
        UpdateUtil.copyNullProperties(seismicSetoutById, entity);
        entity.setUpdateUser(PlatformSessionUtils.getUserId());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        seismicSetoutRepository.updateSeismicSetout(entity);
        return ResponseCreator.createSuccessResponse(entity, "修改成功！");
    }

    /**
     * 修改ScSupplies实体数据
     *
     * @param entity 实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse updateScSupplies(ScSupplies entity) {
        //对数据校验
        StringBuilder message = getMessage2(entity);
        if (message.length() > 0) {
            return RestResponse.fail(String.valueOf(message));
        }
        //把通过Id查询原实体，只赋值到entity目标实体的空值，
        UpdateUtil.copyNullProperties(seismicSetoutRepository.getScSuppliesById(entity.getSuId()), entity);
        entity.setUpdateUser(PlatformSessionUtils.getUserId());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        seismicSetoutRepository.updateScSupplies(entity);
        return ResponseCreator.createSuccessResponse(null, "修改成功！");
    }

    /**
     * 修改ScRescue实体数据
     *
     * @param entity 实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse updateScRescue(ScRescue entity) {
        //对数据校验
        StringBuilder message = getMessage3(entity);
        if (message.length() > 0) {
            return RestResponse.fail(String.valueOf(message));
        }
        //把通过Id查询原实体，只赋值到entity目标实体的空值，
        UpdateUtil.copyNullProperties(seismicSetoutRepository.getScRescueById(entity.getReId()), entity);
        entity.setUpdateUser(PlatformSessionUtils.getUserId());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        seismicSetoutRepository.updateScRescue(entity);
        return ResponseCreator.createSuccessResponse(null, "修改成功！");
    }

    /**
     * 修改ScEquip实体数据
     *
     * @param entity 实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse updateScEquip(ScEquip entity) {
        //对数据校验
        StringBuilder message = getMessage4(entity);
        if (message.length() > 0) {
            return RestResponse.fail(String.valueOf(message));
        }
        //把通过Id查询原实体，只赋值到entity目标实体的空值，
        UpdateUtil.copyNullProperties(seismicSetoutRepository.getScEquipById(entity.getEqId()), entity);
        entity.setUpdateUser(PlatformSessionUtils.getUserId());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        seismicSetoutRepository.updateScEquip(entity);
        return ResponseCreator.createSuccessResponse(null, "修改成功！");
    }

    /**
     * 修改ScSanctuary实体数据
     *
     * @param entity 实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse updateScSanctuary(ScSanctuary entity) {
        //对数据校验
        // StringBuilder message = getMessage5(entity);
        // if (message.length()>0){
        //     return RestResponse.fail(String.valueOf(message));
        // }
        //把通过Id查询原实体，只赋值到entity目标实体的空值，
        UpdateUtil.copyNullProperties(seismicSetoutRepository.getScSanctuaryById(entity.getSaId()), entity);
        entity.setUpdateUser(PlatformSessionUtils.getUserId());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        seismicSetoutRepository.updateScSanctuary(entity);
        return ResponseCreator.createSuccessResponse(null, "修改成功！");
    }

    /**
     * 修改ScMedical实体数据
     *
     * @param entity 实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse updateScMedical(ScMedical entity) {
        //对数据校验
        StringBuilder message = getMessage6(entity);
        if (message.length() > 0) {
            return RestResponse.fail(String.valueOf(message));
        }
        //把通过Id查询原实体，只赋值到entity目标实体的空值，
        UpdateUtil.copyNullProperties(seismicSetoutRepository.getScMedicalById(entity.getMeId()), entity);
        entity.setUpdateUser(PlatformSessionUtils.getUserId());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        seismicSetoutRepository.updateScMedical(entity);
        return ResponseCreator.createSuccessResponse(null, "修改成功！");
    }

    @Override
    public RestResponse saveSeismicSetout1(ScSeismicSetoutVO1 scSeismicSetoutVO1) {
        ScSeismicsetout scSeismicSetout = scSeismicSetoutVO1.getScSeismicSetout();
        RestResponse restResponse = saveSeismicSetout(scSeismicSetout);
        ScSeismicsetout data = (ScSeismicsetout) restResponse.getData();
        if (PlatformObjectUtils.isEmpty(data)) {
            return RestResponse.fail(restResponse.getMessage());
        }
        scSeismicSetoutVO1.setScSeismicSetout(data);
        StringBuilder message = new StringBuilder();
        String scId = data.getScId();
        String provinceId = data.getProvinceId();
        String cityId = data.getCityId();
        String prefectureId = data.getPrefectureId();
        ScSupplies scSupplies = scSeismicSetoutVO1.getScSupplies();
        if (PlatformObjectUtils.isNotEmpty(scSupplies)) {
            scSupplies.setScId(scId);
            RestResponse restResponse1 = saveScSupplies(scSupplies);
            String message1 = restResponse1.getMessage();
            if (!message1.contains("保存成功")) {
                message.append(message1);
            }
            scSeismicSetoutVO1.setScSupplies((ScSupplies) restResponse1.getData());
        }
        ScRescue scRescue = scSeismicSetoutVO1.getScRescue();
        if (PlatformObjectUtils.isNotEmpty(scRescue)) {
            scRescue.setScId(scId);
            RestResponse restResponse1 = saveScRescue(scRescue);
            String message1 = restResponse1.getMessage();
            if (!message1.contains("保存成功")) {
                message.append(message1);
            }
            scSeismicSetoutVO1.setScRescue((ScRescue) restResponse1.getData());
        }
        ScEquip scEquip = scSeismicSetoutVO1.getScEquip();
        if (PlatformObjectUtils.isNotEmpty(scEquip)) {
            scEquip.setScId(scId);
            RestResponse restResponse1 = saveScEquip(scEquip);
            String message1 = restResponse1.getMessage();
            if (!message1.contains("保存成功")) {
                message.append(message1);
            }
            scSeismicSetoutVO1.setScEquip((ScEquip) restResponse1.getData());
        }
        ScSanctuary scSanctuary = scSeismicSetoutVO1.getScSanctuary();
        if (PlatformObjectUtils.isNotEmpty(scSanctuary)) {
            scSanctuary.setScId(scId);
            RestResponse restResponse1 = saveScSanctuary(scSanctuary);
            String message1 = restResponse1.getMessage();
            if (!message1.contains("保存成功")) {
                message.append(message1);
            }
            scSeismicSetoutVO1.setScSanctuary((ScSanctuary) restResponse1.getData());
        }
        ScMedical scMedical = scSeismicSetoutVO1.getScMedical();
        if (PlatformObjectUtils.isNotEmpty(scMedical)) {
            scMedical.setScId(scId);
            RestResponse restResponse1 = saveScMedical(scMedical);
            String message1 = restResponse1.getMessage();
            if (!message1.contains("保存成功")) {
                message.append(message1);
            }
            scSeismicSetoutVO1.setScMedical((ScMedical) restResponse1.getData());
        }
        if (message.length() > 0) {
            return RestResponse.fail(message.toString());
        }
        return ResponseCreator.createSuccessResponse(scSeismicSetoutVO1, "保存成功！");
    }

    @Override
    public RestResponse saveSeismicSetout2(String type, String provinceId, String cityId, String prefectureId, MultipartFile file) {
        ScSeismicsetout scSeismicSetout=new ScSeismicsetout();
        scSeismicSetout.setProvinceId(provinceId);
        scSeismicSetout.setCityId(cityId);
        scSeismicSetout.setPrefectureId(prefectureId);
        scSeismicSetout.setScSfyyjya("0");
        scSeismicSetout.setScSfyyawj("0");
        RestResponse restResponse = saveSeismicSetout(scSeismicSetout);
        ScSeismicsetout data = (ScSeismicsetout) restResponse.getData();
        if (PlatformObjectUtils.isEmpty(data)) {
            return RestResponse.fail(restResponse.getMessage());
        }
        String scId = data.getScId();
        try {
            restResponse = importExcel(type, scId, file);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(" 保存失败！");
        }
        return restResponse;
    }


    /**
     * 新增seismicSetout
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveSeismicSetout(ScSeismicsetout entity) {
        Map<String,String> areaIdCodeMap = (Map<String,String>) JSONObject.parse(redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY  +"areaIdCodeMap"));
        //查询库是否有该省市县的数据
        ScSeismicsetout seismicSetout = seismicSetoutRepository.getSeismicSetoutBydivision(entity.getProvinceId(),
                entity.getCityId(), entity.getPrefectureId());
        if (seismicSetout != null) {
            // RestResponse restResponse = updateSeismicSetout(seismicSetout);
            return ResponseCreator.createSuccessResponse(seismicSetout, "保存成功！");
        }
        entity.setScId(UUIDGenerator.getUUID());
        String divisionNames = null;
        try {
            //根据省市县Id,获取省市县名称;单个id可以获得单个名称;多个id，用逗号分割，可以获得多个名称
            String divisionIds = entity.getProvinceId() + "," + entity.getCityId() + "," + entity.getPrefectureId();
            divisionNames = sysAreaService.getAreaNameByAreaId(divisionIds);
        } catch (Exception e) {
            return RestResponse.fail("省市县的id不符合符合规范！ ");
        }
        String[] split = divisionNames.split(",");
        entity.setProvince(split[0]);
        if (split.length == 2) {
            entity.setCity(split[0]);
            entity.setPrefecture(split[1]);
        } else {
            entity.setCity(split[1]);
            entity.setPrefecture(split[2]);
        }
        //对数据校验
        StringBuilder message = getMessage1(entity);
        entity.setIsDelete(YNEnum.N.toString());
        entity.setCreateUser(PlatformSessionUtils.getUserId());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        entity.setProvinceCode(areaIdCodeMap.get(entity.getProvinceId()));
        entity.setCityCode(areaIdCodeMap.get(entity.getCityId()));
        entity.setPrefectureCode(areaIdCodeMap.get(entity.getPrefectureId()));
        if (message.length() < 1) {
            int i = seismicSetoutRepository.saveSeismicSetout(entity);
            return ResponseCreator.createSuccessResponse(entity, "保存成功！");
        }
        return RestResponse.fail(String.valueOf(message));
    }

    /**
     * 新增ScSupplies
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveScSupplies(ScSupplies entity) {
        ScSeismicsetout seismicSetoutById = seismicSetoutRepository.getSeismicSetoutById(entity.getScId());
        if (PlatformObjectUtils.isEmpty(seismicSetoutById)) {
            return RestResponse.fail("主表尚未有数据，请先添加主表数据！");
        }
        entity.setSuId(UUIDGenerator.getUUID());
        entity.setScId(seismicSetoutById.getScId());
        entity.setIsDelete(YNEnum.N.toString());
        entity.setCreateUser(PlatformSessionUtils.getUserId());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        StringBuilder message = getMessage2(entity);
        if (message.length() < 1) {
            int i = seismicSetoutRepository.saveScSupplies(entity);
            return ResponseCreator.createSuccessResponse(entity, "保存成功！");
        }
        return RestResponse.fail(String.valueOf(message));
    }

    /**
     * 新增ScRescue
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveScRescue(ScRescue entity) {
        ScSeismicsetout seismicSetoutById = seismicSetoutRepository.getSeismicSetoutById(entity.getScId());
        if (PlatformObjectUtils.isEmpty(seismicSetoutById)) {
            return RestResponse.fail("主表尚未有数据，请先添加主表数据！");
        }
        entity.setReId(UUIDGenerator.getUUID());
        entity.setScId(seismicSetoutById.getScId());
        entity.setIsDelete(YNEnum.N.toString());
        entity.setCreateUser(PlatformSessionUtils.getUserId());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        StringBuilder message = getMessage3(entity);
        if (message.length() < 1) {
            int i = seismicSetoutRepository.saveScRescue(entity);
            return ResponseCreator.createSuccessResponse(entity, "保存成功！");
        }
        return RestResponse.fail(String.valueOf(message));
    }

    /**
     * 新增ScEquip
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveScEquip(ScEquip entity) {
        ScSeismicsetout seismicSetoutById = seismicSetoutRepository.getSeismicSetoutById(entity.getScId());
        if (PlatformObjectUtils.isEmpty(seismicSetoutById)) {
            return RestResponse.fail("主表尚未有数据，请先添加主表数据！");
        }
        entity.setEqId(UUIDGenerator.getUUID());
        entity.setScId(seismicSetoutById.getScId());
        entity.setIsDelete(YNEnum.N.toString());
        entity.setCreateUser(PlatformSessionUtils.getUserId());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        StringBuilder message = getMessage4(entity);
        if (message.length() < 1) {
            int i = seismicSetoutRepository.saveScEquip(entity);
            return ResponseCreator.createSuccessResponse(entity, "保存成功！");
        }
        return RestResponse.fail(String.valueOf(message));
    }

    /**
     * 新增ScSanctuary
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveScSanctuary(ScSanctuary entity) {
        ScSeismicsetout seismicSetoutById = seismicSetoutRepository.getSeismicSetoutById(entity.getScId());
        if (PlatformObjectUtils.isEmpty(seismicSetoutById)) {
            return RestResponse.fail("主表尚未有数据，请先添加主表数据！");
        }
        entity.setSaId(UUIDGenerator.getUUID());
        entity.setScId(seismicSetoutById.getScId());
        entity.setIsDelete(YNEnum.N.toString());
        entity.setCreateUser(PlatformSessionUtils.getUserId());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        // StringBuilder message = getMessage(entity);
        // if (message.length()<1){
        int i = seismicSetoutRepository.saveScSanctuary(entity);
        return ResponseCreator.createSuccessResponse(entity, "保存成功！");
        // }
        // return RestResponse.fail(String.valueOf(message));
    }

    /**
     * 新增ScMedical
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveScMedical(ScMedical entity) {
        ScSeismicsetout seismicSetoutById = seismicSetoutRepository.getSeismicSetoutById(entity.getScId());
        if (PlatformObjectUtils.isEmpty(seismicSetoutById)) {
            return RestResponse.fail("主表尚未有数据，请先添加主表数据！");
        }
        entity.setMeId(UUIDGenerator.getUUID());
        entity.setScId(seismicSetoutById.getScId());
        entity.setIsDelete(YNEnum.N.toString());
        entity.setCreateUser(PlatformSessionUtils.getUserId());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        StringBuilder message = getMessage6(entity);
        if (message.length() < 1) {
            int i = seismicSetoutRepository.saveScMedical(entity);
            return ResponseCreator.createSuccessResponse(entity, "保存成功！");
        }
        return RestResponse.fail(String.valueOf(message));
    }

    /**
     * 按条件分页查询
     *
     * @param queryParams 查询的条件
     * @param curPage     第几页
     * @param pageSize    每页的条数
     * @return
     */
    @Override
    public JSONObject getAllSeismicSetout(SeismicSetoutQueryParams queryParams, int curPage, int pageSize) {
        return seismicSetoutRepository.getAllSeismicSetoutParams(queryParams, curPage, pageSize);
    }

    /**
     * 根据id查询SeismicSetout数据
     *
     * @param id
     * @return
     */
    @Override
    public ScSeismicSetoutVO getSeismicSetoutById(String id) {
        ScSeismicsetout seismicSetout = seismicSetoutRepository.getSeismicSetoutById(id);
        ScSeismicSetoutVO scSeismicSetoutVO = new ScSeismicSetoutVO();
        UpdateUtil.copyNullProperties(seismicSetout, scSeismicSetoutVO);
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScMedicalhelpAttachment()))
            scSeismicSetoutVO.setScMedicalhelpAttachment(attachmentInfoService.queryFile(seismicSetout.getScMedicalhelpAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScBzxtjsqkAttachment()))
            scSeismicSetoutVO.setScBzxtjsqkAttachment(attachmentInfoService.queryFile(seismicSetout.getScBzxtjsqkAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScDzyjzztxhyatxAttachment()))
            scSeismicSetoutVO.setScDzyjzztxhyatxAttachment(attachmentInfoService.queryFile(seismicSetout.getScDzyjzztxhyatxAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScDzyjgzjzAttachment()))
            scSeismicSetoutVO.setScDzyjgzjzAttachment(attachmentInfoService.queryFile(seismicSetout.getScDzyjgzjzAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScYjylkzqkAttachment()))
            scSeismicSetoutVO.setScYjylkzqkAttachment(attachmentInfoService.queryFile(seismicSetout.getScYjylkzqkAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScSmxyjbzqkAttachment()))
            scSeismicSetoutVO.setScSmxyjbzqkAttachment(attachmentInfoService.queryFile(seismicSetout.getScSmxyjbzqkAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScCszhffqkAttachment()))
            scSeismicSetoutVO.setScCszhffqkAttachment(attachmentInfoService.queryFile(seismicSetout.getScCszhffqkAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScSfyyjyaAttachment()))
            scSeismicSetoutVO.setScSfyyjyaAttachment(attachmentInfoService.queryFile(seismicSetout.getScSfyyjyaAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScSfyyawjAttachment()))
            scSeismicSetoutVO.setScSfyyawjAttachment(attachmentInfoService.queryFile(seismicSetout.getScSfyyawjAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScYjdcxqAttachment()))
            scSeismicSetoutVO.setScYjdcxqAttachment(attachmentInfoService.queryFile(seismicSetout.getScYjdcxqAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScZdmbbznlAttachment()))
            scSeismicSetoutVO.setScZdmbbznlAttachment(attachmentInfoService.queryFile(seismicSetout.getScZdmbbznlAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScYyyqyjnlAttachment()))
            scSeismicSetoutVO.setScYyyqyjnlAttachment(attachmentInfoService.queryFile(seismicSetout.getScYyyqyjnlAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScKpxjhylAttachment()))
            scSeismicSetoutVO.setScKpxjhylAttachment(attachmentInfoService.queryFile(seismicSetout.getScKpxjhylAttachment()));
        if (PlatformObjectUtils.isNotEmpty(seismicSetout.getScQtdftzyjzbcsAttachment()))
            scSeismicSetoutVO.setScQtdftzyjzbcsAttachment(attachmentInfoService.queryFile(seismicSetout.getScQtdftzyjzbcsAttachment()));
        return scSeismicSetoutVO;
    }

    /**
     * 按scId分页查询
     *
     * @param id       外键Id:scId
     * @param type     类型：1,ScSupplies:应急物资储备;
     *                 2,ScRescue:应急救援力量;
     *                 3,ScEquip:应急救援装备;
     *                 4,ScSanctuary:避难场所;
     *                 5,ScMedical:医疗力量
     * @param curPage  第几页
     * @param pageSize 每页的条数
     * @return
     */
    @Override
    public RestResponse getDetailsById(String id, String type, int curPage, int pageSize) {
        return RestResponse.succeed(seismicSetoutRepository.getDetailsById(id, type, curPage, pageSize));
    }

    /**
     * 据id查询从表数据
     *
     * @param id   主键id
     * @param type 类型：1,ScSupplies:应急物资储备;
     *             2,ScRescue:应急救援力量;
     *             3,ScEquip:应急救援装备;
     *             4,ScSanctuary:避难场所;
     *             5,ScMedical:医疗力量
     * @return
     */
    @Override
    public RestResponse getDetailById(String id, String type) {
        switch (type) {
            case "1":
                return RestResponse.succeed(seismicSetoutRepository.getScSuppliesById(id));
            case "2":
                return RestResponse.succeed(seismicSetoutRepository.getScRescueById(id));
            case "3":
                return RestResponse.succeed(seismicSetoutRepository.getScEquipById(id));
            case "4":
                return RestResponse.succeed(seismicSetoutRepository.getScSanctuaryById(id));
            case "5":
                return RestResponse.succeed(seismicSetoutRepository.getScMedicalById(id));
        }
        return RestResponse.fail("获取失败！");
    }

    /**
     * 逻辑删除，修改数据标志位
     *
     * @param id type 类型：0,ScSeismicsetout:主表;
     *           1,ScSupplies:应急物资储备;
     *           2,ScRescue:应急救援力量;
     *           3,ScEquip:应急救援装备;
     *           4,ScSanctuary:避难场所;
     *           5,ScMedical:医疗力量
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSeismicSetouts(String id, String type) {
        String userId = PlatformSessionUtils.getUserId();
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        if ("0".equals(type)) {
            for (String aType : Arrays.asList("0", "1", "2", "3", "4", "5")) {
                seismicSetoutRepository.deleteSeismicSetoutsById(id, aType, timestamp, userId);
            }
        } else {
            seismicSetoutRepository.delDetails(id, type, timestamp, userId);
        }
    }

    @Override
    public List<SeismicSetout> getAll() {
        return seismicSetoutRepository.getAll();
    }

    /**
     * 对实体类数据进行校验
     *
     * @param entity 实体类
     * @return 错误信息
     */
    private StringBuilder getMessage1(ScSeismicsetout entity) {
        StringBuilder message = new StringBuilder();
        if (PlatformObjectUtils.isNotEmpty(entity.getScMedicalhelp())) {
            if (entity.getScMedicalhelp().length() > 2000) {
                message.append("医疗力量-救护情况 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScBzxtjsqk())) {
            if (entity.getScBzxtjsqk().length() > 2000) {
                message.append("医疗力量-报灾系统建设情况 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScDzyjzztxhyatx())) {
            if (entity.getScDzyjzztxhyatx().length() > 2000) {
                message.append("医疗力量-地震应急组织体系和预案体系 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScDzyjgzjz())) {
            if (entity.getScDzyjgzjz().length() > 2000) {
                message.append("医疗力量-地震应急工作机制 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScYjylkzqk())) {
            if (entity.getScYjylkzqk().length() > 2000) {
                message.append("医疗力量-应急演练开展情况 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScSmxyjbzqk())) {
            if (entity.getScSmxyjbzqk().length() > 2000) {
                message.append("医疗力量-生命线应急保障情况 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScCszhffqk())) {
            if (entity.getScCszhffqk().length() > 2000) {
                message.append("医疗力量-次生灾害防范情况 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScSfyyjya())) {
            message.append("医疗力量-是否有应急预案 不能为空！ ");
        }
        if (PlatformObjectUtils.isEmpty(entity.getScSfyyawj())) {
            message.append("医疗力量-是否有源文件 不能为空！ ");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScYjdcxq())) {
            if (entity.getScYjdcxq().length() > 2000) {
                message.append("医疗力量-应急对策需求 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScZdmbbznl())) {
            if (entity.getScZdmbbznl().length() > 2000) {
                message.append("医疗力量-重点目标保障能力 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScYyyqyjnl())) {
            if (entity.getScYyyqyjnl().length() > 2000) {
                message.append("医疗力量-谣言舆情应急能力 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScKpxjhyl())) {
            if (entity.getScKpxjhyl().length() > 2000) {
                message.append("医疗力量-科普宣教和演练 不能超过2000字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScQtdftzyjzbcs())) {
            if (entity.getScQtdftzyjzbcs().length() > 2000) {
                message.append("医疗力量-其他地地方特征应急准备措施 不能超过2000字！ ");
            }
        }
        return message;
    }

    private StringBuilder getMessage2(ScSupplies entity) {
        StringBuilder message = new StringBuilder();
        if (PlatformObjectUtils.isEmpty(entity.getScSuppliesName())) {
            message.append("应急物资储备-名称 不能为空！ ");
        } else {
            if (entity.getScSuppliesName().length() > 20) {
                message.append("应急物资储备-名称 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScSuppliesBrand())) {
            message.append("应急物资储备-规格型号/品牌 不能为空！ ");
        } else {
            if (entity.getScSuppliesBrand().length() > 20) {
                message.append("应急物资储备-规格型号/品牌 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScSuppliesNumber())) {
            message.append("应急物资储备-数量 不能为空！ ");
        } else {
            try {
                Integer integer = Integer.valueOf(entity.getScSuppliesNumber());
            } catch (Exception e) {
                message.append("应急物资储备-数量 应为数字！ ");
            }
            if (entity.getScSuppliesNumber().length() > 20) {
                message.append("应急物资储备-数量 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScSuppliesUnit())) {
            message.append("应急物资储备-单位 不能为空！ ");
        } else {
            if (entity.getScSuppliesUnit().length() > 20) {
                message.append("应急物资储备-单位 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScSuppliesNote())) {
            if (entity.getScSuppliesNote().length() > 200) {
                message.append("应急物资储备-备注 不能超过200字！ ");
            }
        }
        return message;
    }

    private StringBuilder getMessage3(ScRescue entity) {
        StringBuilder message = new StringBuilder();
        if (PlatformObjectUtils.isEmpty(entity.getScRescueName())) {
            message.append("应急救援力量-队伍名称 不能为空！ ");
        } else {
            if (entity.getScRescueName().length() > 20) {
                message.append("应急救援力量-队伍名称 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScRescueIndustry())) {
            message.append("应急救援力量-所属行业 不能为空！ ");
        } else {
            if (entity.getScRescueIndustry().length() > 20) {
                message.append("应急救援力量-所属行业 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScRescueUnit())) {
            message.append("应急救援力量-隶属单位 不能为空！ ");
        } else {
            if (entity.getScRescueUnit().length() > 20) {
                message.append("应急救援力量-隶属单位 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScRescueAddress())) {
            message.append("应急救援力量-地址 不能为空！ ");
        } else {
            if (entity.getScRescueAddress().length() > 20) {
                message.append("应急救援力量-地址 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScRescueNumber())) {
            message.append("应急救援力量-队伍总人数 不能为空！ ");
        } else {
            try {
                Integer integer = Integer.valueOf(entity.getScRescueNumber());
            } catch (Exception e) {
                message.append("应急救援力量-队伍总人数 应为数字！ ");
            }
            if (entity.getScRescueNumber().length() > 20) {
                message.append("应急救援力量-队伍总人数 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScRescueNote())) {
            if (entity.getScRescueNote().length() > 200) {
                message.append("应急救援力量-备注 不能超过200字！ ");
            }
        }
        return message;
    }

    private StringBuilder getMessage4(ScEquip entity) {
        StringBuilder message = new StringBuilder();
        if (PlatformObjectUtils.isEmpty(entity.getScEquipName())) {
            message.append("应急装备体系-装备名称 不能为空！ ");
        } else {
            if (entity.getScEquipName().length() > 20) {
                message.append("应急装备体系-装备名称 不能超过20字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScEquipNumber())) {
            message.append("应急装备体系-数量(台) 不能为空！ ");
        }else {
            try {
                Integer integer = Integer.valueOf(entity.getScEquipNumber());
            } catch (Exception e) {
                message.append("应急装备体系-数量(台) 应为数字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScEquipNote())) {
            if (entity.getScEquipNote().length() > 200) {
                message.append("应急装备体系-备注 不能超过200字！ ");
            }
        }
        return message;
    }

    private StringBuilder getMessage6(ScMedical entity) {
        StringBuilder message = new StringBuilder();
        if (PlatformObjectUtils.isEmpty(entity.getScMedicalName())) {
            message.append("医疗力量-单位名称 不能为空！ ");
        } else {
            if (entity.getScMedicalName().length() > 50) {
                message.append("医疗力量-单位名称 不能超过50字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScMedicalDoctor())) {
            message.append("医疗力量-医生人数 不能为空！ ");
        }else {
            try {
                Integer integer = Integer.valueOf(entity.getScMedicalDoctor());
            } catch (Exception e) {
                message.append("医疗力量-医生人数 应为数字！ ");
            }
        }
        if (PlatformObjectUtils.isEmpty(entity.getScMedicalNurse())) {
            message.append("医疗力量-护士人数 不能为空！ ");
        }else {
            try {
                Integer integer = Integer.valueOf(entity.getScMedicalNurse());
            } catch (Exception e) {
                message.append("医疗力量-医生人数 应为数字！ ");
            }
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getScMedicalNote())) {
            if (entity.getScMedicalNote().length() > 200) {
                message.append("医疗力量-备注 不能超过200字！ ");
            }
        }
        return message;
    }

    /**
     * 导入
     *
     * @param type 类型：1,ScSupplies:应急物资储备;
     *             2,ScRescue:应急救援力量;
     *             3,ScEquip:应急救援装备;
     *             4,ScSanctuary:避难场所;
     *             5,ScMedical:医疗力量
     * @param file 导入的文件
     * @throws IOException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse importExcel(String type, String scId, MultipartFile file) {
        RestResponse restResponse = null;
        try {
            if ("1".equals(type)) {
                List<ScSupplies> list = FileUtil.importExcel(file, 0, 1, ScSupplies.class);
                for (int i = 0; i < list.size(); i++) {
                    if (CheckObjFields.isAllFieldNull(list.get(i))) {
                        continue;
                    }
                    list.get(i).setScId(scId);
                    restResponse = saveScSupplies(list.get(i));
                }
            } else if ("2".equals(type)) {
                List<ScRescue> list = FileUtil.importExcel(file, 0, 1, ScRescue.class);
                for (int i = 0; i < list.size(); i++) {
                    if (CheckObjFields.isAllFieldNull(list.get(i))) {
                        continue;
                    }
                    list.get(i).setScId(scId);
                    restResponse = saveScRescue(list.get(i));
                }
            } else if ("3".equals(type)) {
                List<ScEquip> list = FileUtil.importExcel(file, 0, 1, ScEquip.class);
                for (int i = 0; i < list.size(); i++) {
                    if (CheckObjFields.isAllFieldNull(list.get(i))) {
                        continue;
                    }
                    list.get(i).setScId(scId);
                    restResponse = saveScEquip(list.get(i));
                }
            } else if ("4".equals(type)) {
                List<ScSanctuary> list = FileUtil.importExcel(file, 0, 1, ScSanctuary.class);
                for (int i = 0; i < list.size(); i++) {
                    if (CheckObjFields.isAllFieldNull(list.get(i))) {
                        continue;
                    }
                    list.get(i).setScId(scId);
                    restResponse = saveScSanctuary(list.get(i));
                }
            } else if ("5".equals(type)) {
                List<ScMedical> list = FileUtil.importExcel(file, 0, 1, ScMedical.class);
                for (int i = 0; i < list.size(); i++) {
                    if (CheckObjFields.isAllFieldNull(list.get(i))) {
                        continue;
                    }
                    list.get(i).setScId(scId);
                    restResponse = saveScMedical(list.get(i));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            restResponse = RestResponse.fail(" 保存失败！");
        }
        return restResponse;
    }

    @Override
    public ScSeismicsetout findByAreaId(String areaId) {
        ScSeismicsetout scSeismicsetout=seismicSetoutRepository.findByAreaId(areaId);
        return scSeismicsetout;
    }

    @Override
    public Integer countByCounty(String province, String city, String county) {
        return seismicSetoutRepository.countByCounty(province, city, county);
    }

    @Override
    public Integer countByCity(String province, String city) {
        return seismicSetoutRepository.countByCity(province, city);
    }

    @Override
    public Integer countByProvince(String name) {
        return seismicSetoutRepository.countByProvince(name);
    }

    @Override
    public Integer countByProvinceAndYear(String provinceName, String taskYear) {
        return seismicSetoutRepository.countByProvinceAndYear(provinceName, taskYear);
    }
}
